namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorADTM : BaseIndicator
    {
        private string adtmField = Common.GetGuid();
        private string dbmField = ("ADTM.DBM." + Common.GetGuid());
        private string dbmSumField = ("ADTM.DBMSUM." + Common.GetGuid());
        private string dtmField = ("ADTM.DTM." + Common.GetGuid());
        private string dtmSumField = ("ADTM.DTMSUM." + Common.GetGuid());
        private LineShape tlsMa1;
        private LineShape tlsMa2;

        public IndicatorADTM()
        {
            base.IndType = IndicatorType.ADTM;
            base.IndName = "ADTM";
            base.IndDesc = "动态买卖气指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 23.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "M", 8.0));
            base.SourceDic = new Dictionary<string, string>();
            base.SourceDic["Open"] = "";
            base.SourceDic["High"] = "";
            base.SourceDic["Low"] = "";
            base.fields = new string[] { "MA1", "MA2" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.tlsMa1) && (obj != this.tlsMa2))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double num = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Open"]]);
            double num2 = 0.0;
            double num3 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["High"]]);
            double num4 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Low"]]);
            if (r > 0)
            {
                num2 = Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][base.SourceDic["Open"]]);
            }
            double curValue = 0.0;
            double num6 = 0.0;
            if (num > num2)
            {
                curValue = ((num3 - num) >= (num - num2)) ? (num3 - num) : (num - num2);
            }
            if (num < num2)
            {
                num6 = ((num - num4) >= Math.Abs((double) (num - num2))) ? (num - num4) : Math.Abs((double) (num - num2));
            }
            double num7 = Common.CalcutaSumilationValue(r, this.dtmSumField, (int) base.IndPeriod[0].ParamValue, this.dtmField, curValue, base.Crab.DataSource);
            double num8 = Common.CalcutaSumilationValue(r, this.dbmSumField, (int) base.IndPeriod[0].ParamValue, this.dbmField, num6, base.Crab.DataSource);
            double num9 = 0.0;
            if (num7 > num8)
            {
                num9 = (num7 - num8) / num7;
            }
            else if (num7 < num8)
            {
                num9 = (num7 - num8) / num8;
            }
            double num10 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[1].ParamValue, this.tlsMa1.Name, this.adtmField, num9, base.Crab.DataSource);
            double num11 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[1].ParamValue, this.tlsMa2.Name, this.tlsMa1.Name, num10, base.Crab.DataSource);
            return new double[] { curValue, num6, num7, num8, num9, num10, num11 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.TlsMa1, this.tlsMa2 };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.tlsMa1);
            base.Crab.DeleteShape(this.tlsMa2);
            base.Crab.DataSource.Columns.Remove(this.dtmField);
            base.Crab.DataSource.Columns.Remove(this.dbmField);
            base.Crab.DataSource.Columns.Remove(this.dtmSumField);
            base.Crab.DataSource.Columns.Remove(this.dbmSumField);
            base.Crab.DataSource.Columns.Remove(this.adtmField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.dtmField, numArray[0], r);
                        base.Crab.SetValue(this.dbmField, numArray[1], r);
                        base.Crab.SetValue(this.dtmSumField, numArray[2], r);
                        base.Crab.SetValue(this.dbmSumField, numArray[3], r);
                        base.Crab.SetValue(this.adtmField, numArray[4], r);
                        base.Crab.SetValue(this.tlsMa1.Name, numArray[5], r);
                        base.Crab.SetValue(this.tlsMa2.Name, numArray[6], r);
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.tlsMa1 = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.tlsMa2 = base.Crab.AddLine(fields[1], panelID);
            base.SourceDic["Open"] = source[0];
            base.SourceDic["High"] = source[1];
            base.SourceDic["Low"] = source[2];
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            base.Crab.DataSource.Columns.Add(this.dtmField);
            base.Crab.DataSource.Columns.Add(this.DbmField);
            base.Crab.DataSource.Columns.Add(this.dtmSumField);
            base.Crab.DataSource.Columns.Add(this.dbmSumField);
            base.Crab.DataSource.Columns.Add(this.adtmField);
        }

        public string AdtmField
        {
            get
            {
                return this.adtmField;
            }
            set
            {
                this.adtmField = value;
            }
        }

        public string DbmField
        {
            get
            {
                return this.dbmField;
            }
            set
            {
                this.dbmField = value;
            }
        }

        public string DbmSumField
        {
            get
            {
                return this.dbmSumField;
            }
            set
            {
                this.dbmSumField = value;
            }
        }

        public string DtmField
        {
            get
            {
                return this.dtmField;
            }
            set
            {
                this.dtmField = value;
            }
        }

        public string DtmSumField
        {
            get
            {
                return this.dtmSumField;
            }
            set
            {
                this.dtmSumField = value;
            }
        }

        public LineShape TlsMa1
        {
            get
            {
                return this.tlsMa1;
            }
            set
            {
                this.tlsMa1 = value;
            }
        }

        public LineShape TlsMa2
        {
            get
            {
                return this.tlsMa2;
            }
            set
            {
                this.tlsMa2 = value;
            }
        }
    }
}

